// pages/addressedit/addressedit.js
const Protocol = require("../../Controls/Protocol")
const util = require("../../utils/util")
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast.js'
import area from "../../pages/addressedit/area.js"

const app = getApp();

const handler = 
{
  success: (res)=>
  {
    wx.navigateBack({
      
    })
  },

  fail:(res)=>
  {

  }
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:"",
    phonenum:"",
    city:"",
    detail:"", 

    area: area,
    value: 110101,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.addressID = options.addressID;

    if(this.addressID)
    {
      for(var i = 0; i < app.globalData.address.length; i++)
      {
        if(app.globalData.address[i].id == this.addressID)
        {
          console.log(app.globalData.address[i]);
          this.setData(app.globalData.address[i]);
          break;
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  btnSelectIvk()
  { 
    this.setData({isPop:true});
  },

  btnOkIvk(args)
  {
    var arr = new Array();
    for(var i = 0; i < args.detail.values.length; i++)
    {
      arr.push(args.detail.values[i].name);
    }

    this.setData({ isPop: false, city:arr.join(" , ")});
  },

  btnCancelIvk()
  {
    this.setData({isPop:false});
  },

  onCityChangeIvk(args)
  {

  },

  formSubmit:function(e)
  {
    console.log(e);
    if (e.detail.value.name.length == 0) {
      Toast('收货人不能为空');
      return;
    }

    if (e.detail.value.phonenum.length == 0) {
      Toast("联系电话不能为空");
      return;
    }

    if(this.data.city == 0)
    {
      Toast('城市不能为空');
      return;
    }

    if (e.detail.value.detail.length == 0) {
      Toast('详细地址不能为空');
      return;
    }

    if (this.addressID) {
      var header = Object.assign(e.detail.value, { id: parseInt(this.addressID), city: this.data.city});
      Protocol.editAddress(header, handler);
    }
    else {
      var header = Object.assign(e.detail.value, {city: this.data.city});
      Protocol.createAddress(e.detail.value, handler);
    }
  },
})